from flask import Blueprint,request,json,jsonify
from application.models.users.user import *
user_blue = Blueprint("userblue",__name__)

@user_blue.route("/addgood_type",methods=['POST'])
def addgood_type():
    data = json.loads(request.data)
    name = data['name']

    typedata = Good_Type(name=name)
    db.session.add(typedata)
    db.session.commit()

    return jsonify({'code':200,"mes":"添加分类成功"})

@user_blue.route('/gettype')
def gettype():
    typedata = Good_Type.query.all()
    list = [{
        'id':i.id,
        'name':i.name
    }for i in typedata]

    return jsonify({'code':200,"list":list})

@user_blue.route('/addgoods',methods=['POST'])
def addgoods():
    data = json.loads(request.data)
    name = data['name']
    price = data['price']
    url = data['url']
    gid = data['gid']

    goodsdata = Goods(name=name,price=price,url=url,gid=gid)
    db.session.add(goodsdata)
    db.session.commit()

    return jsonify({'code':200,"mes":"添加商品成功"})


import math
@user_blue.route('/getgoods')
def getgoods():
    t_id = request.args.get('id')
    # page = int(request.args.get('page'))
    # total = Goods.query.count()
    # page_size = 1
    # start = math.ceil((page-1)*page_size)
    # .offset(start).limit(page_size)
    if t_id:
        goods = Goods.query.filter(Goods.gid == t_id)
    else:
        goods = Goods.query.all()
    list = [{
        'id': i.id,
        'name': i.name,
        'price':i.price,
        'url':i.url,
        'gid':i.gid
    } for i in goods]
    # , 'total': total, 'page_size': page_size
    return jsonify({'code': 200, "list": list})

@user_blue.route('/getgood/<int:id>')
def getgood(id):
    goods = Goods.query.filter_by(id=id)
    list = []
    for i in goods:
        list.append({
            'id': goods.id,
            'name': goods.name,
            'price': goods.price,
            'url': goods.url,
            'gid': goods.gid
        })
    return jsonify({'code': 200,"list": list})

@user_blue.route('/update_good',methods=['PUT'])
def up_good():
    data = json.loads(request.data)
    Goods.query.filter(Goods.id == data['id']).update({
        'id':data['id'],
        'name':data['name'],
        'price':data['price'],
        'url':data['url'],
        'gid':data['gid']
    })
    db.session.commit()
    return jsonify({'code':200,'mes':"修改成功"})

@user_blue.route('/del_good',methods=['DELETE'])
def delgood():
    id = request.args.get('id')
    Goods.query.filter(Goods.id == id).delete()
    db.session.commit()
    return jsonify({'code':200,'mes':'删除商品成功'})

import os
# @user_blue.route('/upload',methods=['POST'])
# def upload():
#     # 获取文件
#     file = request.files.get("file")
#     filename = file.filename
#     path = os.path.join("static/upload/" + filename)
#     file.save(path)
#     return jsonify({'code':200,"url":path})

















@user_blue.route('/upload',methods=['POST'])
def upload():
    file = request.files.get('file')
    filename = file.filename
    path = os.path.























